package com.wechatapp.mengyao.poetry.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wechatapp.mengyao.poetry.core.PageResult;
import com.wechatapp.mengyao.poetry.entity.Album;
import com.wechatapp.mengyao.poetry.mapper.AlbumMapper;
import com.wechatapp.mengyao.poetry.service.AlbumService;
import com.wechatapp.mengyao.poetry.vo.AlbumView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yanghuaxu
 * @date 2025/7/5 16:36
 */
@Service
@Slf4j
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public PageResult<AlbumView> getAlbumListByCategoryId(Map<String, String> map) {
        int pageNum = Integer.parseInt(map.getOrDefault("pageNum", "1"));
        int pageSize = Integer.parseInt(map.getOrDefault("pageSize", "10"));
        Assert.isTrue(map.containsKey("categoryId"), "缺少必填参数categoryId");
        PageHelper.startPage(pageNum, pageSize, true);
        Page<Album> page = (Page<Album>) albumMapper.getAlbumListByCategoryId(map);
        List<AlbumView> result = page.getResult().stream().map(album -> new AlbumView(album.getId(), album.getCategoryId(), album.getName(), album.getOriginalName(), album.getSortOrder(), album.getEpisodeCount(), album.getCoverUrl(), album.getMediaType())).collect(Collectors.toList());
        return PageResult.fromPage(page, result);
    }
}
